package lanxing.chances.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import lanxing.chances.util.JSONUtils;

/**
 * @author <a href="mailto:lanxing@chances.com.cn">lanxing</a>
 * @version 2019年1月30日
 *
 */
public class TransferXMLToJSONNC {

    public static String xml2jsonString() throws JSONException, IOException {
                                                                                         
        File file = new File("D:\\eclipse-workspace\\UtilsProject\\src\\main\\resources\\manifestRankList.xml");
        // FileInputStream fis = new FileInputStream(f);
        // String xml = IOUtils.toString(fis);

        BufferedReader br = new BufferedReader(new FileReader(file));

        StringBuffer sb = new StringBuffer();
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            System.out.println(line);
        }

        JSONObject xmlJSONObj = XML.toJSONObject(sb.toString());
        String xmlString = xmlJSONObj.toString();

        // 先替换字段
        xmlString = xmlString.replace("\"desc\"", "\"title\"");
        xmlString = xmlString.replace("\"template\"", "\"templates\"");
        xmlString = xmlString.replace("\"biz\"", "\"nav\"");
        xmlString = xmlString.replace("\"param\"", "\"params\"");
        xmlString = xmlString.replace("\"label\":\"\"","\"templateType\": null");
        
        xmlString = xmlString.replace("\"template_path\":\"","\"name\": \"templates/");
        xmlString = xmlString.replace("\"preview_path\":\"","\"preview\": \"templates/");
        
        // 过滤层级
        Map<String, Object> xmlMap = JSONUtils.toMap(xmlString);
        @SuppressWarnings("unchecked")
        Map<String,Object> templatePackage = (Map<String, Object>) xmlMap.get("TemplatePackage");
        
        //没有params参数的模板得加上params
        @SuppressWarnings("unchecked")
        List<Map<String,Object>> templates = (List<Map<String, Object>>) templatePackage.get("templates");
        templates.forEach(template ->{
            Object params = template.get("params");
            if(params == null) {
                template.put("params", new ArrayList<Object>());
            }else {
                if(params instanceof Map) {
                    Map<String,Object> param = (Map<String,Object>)params;

                    Object value = param.get("value"); 
                    if(value == null) {
                        param.put("value", "");
                    }

                    List<Map<String,Object>> newList = new ArrayList<Map<String,Object>>();
                    newList.add(param);                    
                    template.put("params", newList);
                }else {
                    @SuppressWarnings("unchecked")
                    List<Map<String,Object>> paramsList = (List<Map<String, Object>>) params;
                    paramsList.forEach(param ->{
                        Object value = param.get("value"); 
                        if(value == null) {
                            param.put("value", "");
                        }
                    });                    
                }
            }
            
            Object templateType = template.get("type");
            template.remove("type");
            template.put("bindType", templateType);
        });

        return JSONUtils.fromObject(templatePackage);

    }

    public static void main(String[] args) throws JSONException, IOException {

        String string = xml2jsonString();
        System.out.println(string);

    }
}
